import { defineComponent, watch, reactive } from "vue";
import { createNamespace } from "../utils";
import FanPicker from "../picker/picker.js";
const __vue_sfc__ = defineComponent({
  name: createNamespace("DatePicker")[0],
  options: { virtualHost: true },
  emits: ["update:show", "update:modelValue", "confirm", "cancel"],
  components: { FanPicker },
  props: {
    modelValue: {
      type: Array,
      default: () => []
    },
    show: {
      type: Boolean,
      default: false
    },
    title: String,
    minDate: Date,
    maxDate: Date,
    filter: {
      type: [Function, null],
      default: null
    },
    cancelText: {
      type: String,
      default: "\u53D6\u6D88"
    },
    confirmText: {
      type: String,
      default: "\u786E\u8BA4"
    }
  },
  setup(props, { emit }) {
    const state = reactive({
      innerShow: props.show,
      value: props.modelValue,
      list: []
    });
    const formatDate = (list) => {
      if (props.filter)
        state.list = props.filter(list);
      else
        state.list = list;
    };
    const d = /* @__PURE__ */ new Date();
    let minYear = d.getFullYear() - 10;
    let maxYear = d.getFullYear() + 10;
    const formatItem = (num, type, i, isSub) => {
      return Array.from({ length: num }, (_, index) => ({
        text: index + 1 < 10 ? `0${index + 1}` : String(index + 1),
        id: index + 1 < 10 ? `0${index + 1}` : String(index + 1),
        type,
        children: isSub ? fillDate(minYear + i, index + 1) : []
      }));
    };
    const init = () => {
      if (props.minDate)
        minYear = props.minDate.getFullYear();
      if (props.maxDate)
        maxYear = props.maxDate.getFullYear();
      if (minYear > maxYear) {
        const min = maxYear;
        maxYear = minYear;
        minYear = min;
      }
      const num = maxYear - minYear;
      const list = [];
      for (let i = 0; i <= num; i++) {
        list.push({
          text: String(minYear + i),
          id: String(minYear + i),
          type: "year",
          children: formatItem(12, "month", i, true)
        });
      }
      formatDate(list);
    };
    const fillDate = (year, month) => {
      const lm = [1, 3, 5, 7, 8, 10, 12];
      const sm = [4, 6, 9, 11];
      if (lm.includes(month))
        return formatItem(31, "day");
      else if (sm.includes(month))
        return formatItem(30, "day");
      else if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0)
        return formatItem(29, "day");
      else
        return formatItem(28, "day");
    };
    const onConfirm = (val) => emit("confirm", val);
    const onCancel = () => emit("cancel");
    watch(props.modelValue, (val) => {
      state.value = [...val];
    }, { deep: true });
    watch(() => state.value, (val) => {
      emit("update:modelValue", val);
    }, { deep: true });
    watch(() => props.show, (val) => state.innerShow = val);
    watch(() => state.innerShow, (val) => emit("update:show", val));
    init();
    return { state, onConfirm, onCancel };
  }
});
import { renderSlot as _renderSlot, resolveComponent as _resolveComponent, withCtx as _withCtx, createSlots as _createSlots, openBlock as _openBlock, createBlock as _createBlock } from "vue";
function __vue_render__(_ctx, _cache) {
  const _component_FanPicker = _resolveComponent("FanPicker");
  return _openBlock(), _createBlock(_component_FanPicker, {
    modelValue: _ctx.state.value,
    "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => _ctx.state.value = $event),
    show: _ctx.state.innerShow,
    "onUpdate:show": _cache[1] || (_cache[1] = ($event) => _ctx.state.innerShow = $event),
    data: _ctx.state.list,
    "value-key": "id",
    title: _ctx.title,
    "cancel-text": _ctx.cancelText,
    "confirm-text": _ctx.confirmText,
    onConfirm: _ctx.onConfirm,
    onCancel: _ctx.onCancel
  }, _createSlots({
    _: 2
    /* DYNAMIC */
  }, [
    _ctx.$slots.default ? {
      name: "header",
      fn: _withCtx(() => [
        _renderSlot(_ctx.$slots, "default")
      ]),
      key: "0"
    } : void 0
  ]), 1032, ["modelValue", "show", "data", "title", "cancel-text", "confirm-text", "onConfirm", "onCancel"]);
}
__vue_sfc__.render = __vue_render__;
var stdin_default = __vue_sfc__;
export {
  stdin_default as default
};
